package images

import (
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `images` (
//  `id` int NOT NULL AUTO_INCREMENT,
//  `image_name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `image_tag` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
//  `android_version` int DEFAULT NULL,
//  `created_at` datetime DEFAULT NULL,
//  `updated_at` datetime DEFAULT NULL,
//  `status` tinyint(1) DEFAULT '1',
//  PRIMARY KEY (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

type Images struct {
	Id             int       `json:"id" gorm:"primary_key;column:id"`
	ImageName      string    `json:"image_name" gorm:"column:image_name"`
	ImageTag       string    `json:"image_tag" gorm:"column:image_tag"`
	AndroidVersion int       `json:"android_version" gorm:"column:android_version"`
	CreatedAt      time.Time `json:"created_at" gorm:"column:created_at"`
	UpdatedAt      time.Time `json:"updated_at" gorm:"column:updated_at"`
	Status         int       `json:"status" gorm:"column:status"`
}

func (i *Images) TableName() string {
	return "images"
}
func (i *Images) Create(db *gorm.DB) error {
	return db.Create(i).Error
}
func (i *Images) CreateOrUpdate(db *gorm.DB) error {
	return db.Save(i).Error
}
func (i *Images) Update(db *gorm.DB) error {
	return db.Update(i).Error
}
func (i *Images) GetById(db *gorm.DB) error {
	return db.Where("id = ?", i.Id).First(i).Error
}
func (i *Images) GetList(db *gorm.DB, offset int, limit int, search map[string]interface{}) ([]Images, int, error) {
	var list []Images
	var total int
	query := db.Model(&Images{})
	for k, v := range search {
		query = query.Where(k+" = ?", v)
	}
	query.Count(&total)
	err := query.Offset(offset).Limit(limit).Find(&list).Error
	return list, total, err
}
func (i *Images) GetAll(db *gorm.DB) ([]Images, error) {
	var list []Images
	return list, db.Order("id asc").Where("status = ?", 1).Find(&list).Error
}
func (i *Images) GetAndroidVersion(db *gorm.DB) ([]Images, error) {
	var list []Images
	return list, db.Table(i.TableName()).Where("status = ?", 1).Group("android_version").Order("android_version asc").Find(&list).Error
}
